<?php

namespace app\admin\controller;

use think\Controller;
use app\common\model\PicTypes;
use app\common\model\PicLists as M;
use think\facade\Request;
//广告位列表
class Piclists extends Controller
{

    //图片列表
    public function index()
    {
        $p       = new PicTypes();
        $pictype = $p->getAllPitType();
        $this->assign('pictype', $pictype);
        return $this->fetch();
    }

    //类别josn数据
    public function getpic_josn()
    {
        $p        = new PicTypes();
        $pic      = new M();
        $piclist  = $pic->getPicList(1);
        $piccount = $pic->getPicList(2);
        $count    = count($piccount);
        $lists    = ['code' => 0, 'msg' => '', 'count' => $count, 'data' => []];
        foreach ($piclist as $k => $v)
        {
            $v['type_id']    = $p->getByName($v['type_id']);
            $pic             = $v['pic'];
            $v['pic']        = "<a href = '$pic' target='_blank'><img style='height: 30px;' src = '$pic'></a>";
            $lists['data'][] = $v;
        }
        return $lists;
    }

    //图片修改页面
    public function edit()
    {
        $pic     = new M();
        $p       = new PicTypes();
        $pictype = $p->getAllPitType();
        $this->assign('pictype', $pictype);
        $s       = $pic->getById(input('id'));
        $this->assign('id', $s->id);
        $this->assign('s', $s);
        return $this->fetch();
    }

    //图片添加页面
    public function add()
    {
        $p       = new PicTypes();
        $pictype = $p->getAllPitType();
        $this->assign('pictype', $pictype);
        return $this->fetch();
    }

    //多图片添加页面
    public function adds()
    {
        $p       = new PicTypes();
        $pictype = $p->getAllPitType();
        $this->assign('pictype', $pictype);
        return $this->fetch();
    }

    public function pics_submit()
    {
        $m = new M();
        if (input('id', 0) == 0)
        {
            if (input('post.type_id') == '')
            {
                return js_return(0, "图片类别不能为空");
            }
            $pics = input('post.gallery/a');
            if(empty($pics)){
                return js_return(0, "图片不能为空");
            }
            foreach ($pics as $pic){
                $opts[]  = [
                    //                'name'    => input('name'), //名称
                    //                'note'    => input('note'), //备注
                    'pic'     => $pic, //图片
                    'type_id' => input('type_id'), //图片类别
                    'sort'    => input('sort'), //排序
                    //                'url'     => input('url'), //链接地址
                ];
            }
            $rs   = $m->addAll($opts);
            //日志
            return js_return($rs[0], $rs[1]);
        }
    }


    //图片添加
    public function piclist_submit()
    {
        $pic = new M();
        if (input('id', 0) == 0)
        {
            if (input('post.type_id') == '')
            {
                return js_return(0, "图片类别不能为空");
            }
            if (input('post.pic') == '')
            {
                return js_return(0, "图片不能为空");
            }
            $opt  = [
//                'name'    => input('name'), //名称
//                'note'    => input('note'), //备注
                'pic'     => input('pic'), //图片
                'type_id' => input('type_id'), //图片类别
                'sort'    => input('sort'), //排序
//                'url'     => input('url'), //链接地址
            ];
            $rs   = $pic->add($opt);
            //日志
            $id   = $pic->order("id desc")->find();
            $name = session('my_self')['name'];
            $des  = $name . "【添加】了编号为【" . $id->id . "】的图片";
            add_log(2, session('my_self')['id'], $des);
            return js_return($rs[0], $rs[1]);
        }
        else
        {
            $opt  = [
//               'name'    => input('name'), //名称
//                'note'    => input('note'), //备注
                'pic'     => input('pic'), //图片
                'type_id' => input('type_id'), //图片类别
//                'sort'    => input('sort'), //排序
//                'url'     => input('url'), //链接地址
            ];
            $rs   = $pic->modify($opt, input('id'));
            //日志
            $id   = input('post.id');
            $name = session('my_self')['name'];
            $des  = $name . "【修改】了编号为【" . $id . "】的图片";
            add_log(2, session('my_self')['id'], $des);
            return js_return($rs[0], $rs[1]);
        }
    }

    //图片删除
    public function del_submit()
    {
        $pic  = new M();
        $pic->del();
        //日志
        $id   = input('id');
        $name = session('my_self')['name'];
        $des  = $name . "【删除】了编号为【" . $id . "】的图片";
        add_log(2, session('my_self')['id'], $des);
        $this->success('新增成功', 'Piclists/');
    }

}
